//
//  ModalView.m
//  CCEnglish
//
//  Created by mac on 13-6-4.
//  Copyright (c) 2013年 mac. All rights reserved.
//

#import "ModalView.h"

@implementation ModalView


- (void)keyboardWillShow:(id)sender
{
    [self moveToUpSide];
}

- (void)keyboardWillHide:(id)sender
{
    [self moveToCenterSide];
}

-(void)pageStart
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];

    handleView = [[UIApplication sharedApplication] keyWindow].rootViewController.view;
    keyView = self;
    [handleView addSubview:keyView];
    
    blackView = [[UIControl alloc]initWithFrame:handleView.bounds];
    blackView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5f];
    [blackView addTarget:self action:@selector(moveToDownSide) forControlEvents:UIControlEventTouchUpInside];
    [handleView insertSubview:blackView belowSubview:self];
    
    keyView.frame = CGRectMake((320-keyView.frame.size.width)/2,
                               handleView.frame.size.height,
                               keyView.frame.size.width,
                               keyView.frame.size.height);
    
    [self moveToCenterSide];
}



- (void)moveToUpSide {
    [self animateHomeViewToSide:CGRectMake(keyView.frame.origin.x,
                                           handleView.frame.size.height-252 - keyView.frame.size.height,
                                           keyView.frame.size.width,
                                           keyView.frame.size.height)];
}


- (void)moveToCenterSide {
    [self animateHomeViewToSide:CGRectMake(keyView.frame.origin.x,
                                           (handleView.frame.size.height-keyView.frame.size.height)/2,
                                           keyView.frame.size.width,
                                           keyView.frame.size.height)];
}


// move view to right side
- (void)moveToDownSide {
    [self animateHomeViewToSide:CGRectMake(keyView.frame.origin.x,
                                           handleView.frame.size.height,
                                           keyView.frame.size.width,
                                           keyView.frame.size.height)];
}

// animate home view to side rect
- (void)animateHomeViewToSide:(CGRect)newViewRect {
    [UIView animateWithDuration:0.2
                     animations:^{
                         keyView.frame = newViewRect;
                     }
                     completion:^(BOOL finished){
                         if (keyView.frame.origin.y == handleView.frame.size.height) {
                             [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
                             [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
                             if (blackView) {
                                 [blackView removeFromSuperview];
                                 blackView = nil;
                                 [self removeFromSuperview];
                             }
                         }
                         
                     }
     ];
}

@end
